<?php

class sf2s3iGuardUser extends sfBasicSecurityUser {

  public function signIn($user, $remember = false)
  {
    $this->setAuthenticated(true);
    $perms = $user->getCredentials();
    foreach ($perms as $perm)
    {
      $this->addCredential($perm);
    }

    sfContext::getInstance()->getUser()->setAttribute('user_login', $user->getLogin());
    sfContext::getInstance()->getUser()->setAttribute('user_id', $user->getId());
    
    if ($remember)
    {
      // creation d'une chaine aleatoire si non existant
      if (!$user->getRememberKey())
      {
        $remember_key = GuardUser::generate_random_key();
        $user->setRememberKey($remember_key);
        $user->save();
      }

      $value = base64_encode(serialize(array($user->getRememberKey(), $user->getLogin())));
      sfContext::getInstance()->getResponse()->setCookie('sfGuardCookie', $value, time()+60*60*24*15, '/');
    }
  }

  public function signOut()
  {
    $this->setAuthenticated(false);
    $this->clearCredentials();
    sfContext::getInstance()->getUser()->getAttributeHolder()->clear();
    sfContext::getInstance()->getResponse()->setCookie('sfGuardCookie', '', time() - 3600, '/');
  }
}
